package com.cloudxingkong.dianyinke.Student.Ui;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.cloudxingkong.dianyinke.NewTeacher.Utils.CookieHelper;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Student.Adapter.VideoTeachingAdapter;
import com.cloudxingkong.dianyinke.Student.bean.VideoTeacherEntity;
import com.cloudxingkong.dianyinke.Uri.studentUri.StudentUri;
import com.cloudxingkong.dianyinke.Uri.teacherUri.ThisUri;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * @视频教学Activity
 * @name:Zxrui
 */
public class VideoTeaching extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    private ListView lv_videoTeaching;
    private List<VideoTeacherEntity.ListBean> list;
    private SharedPreferences sharedPreferences;
    private SwipeRefreshLayout swipeRefreshLayout;
    private ProgressDialog progressDialog;
    private VideoTeachingAdapter adapter;

    @Override
    protected void onCreateView(Bundle savedInstanceState) {
        super.onCreateView(savedInstanceState);
        setContentView(R.layout.activity_video_teaching);
        init();
        loadData();
        lv_videoTeaching.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(VideoTeaching.this, VideoTeachingDetail.class);
                intent.putExtra("vTitle", list.get(position).getTitle());
                intent.putExtra("vMemname", list.get(position).getMemname());
                intent.putExtra("vViews", list.get(position).getViews() + "");
                intent.putExtra("vLikes", list.get(position).getLikes() + "");
                intent.putExtra("vNote", list.get(position).getNote());
                intent.putExtra("vPath", list.get(position).getPath());
                intent.putExtra("vID", list.get(position).getId() + "");
                intent.putExtra("pic", list.get(position).getAvatar());
                intent.putExtra("vTime", list.get(position).getUploadtime() + "");
                intent.putExtra("pic2", list.get(position).getCoverpic() + "");
                startActivity(intent);
            }
        });


    }

    private void loadData() {

        final String sessionid = CookieHelper.getSessionID(sharedPreferences);
        RequestParams params = new RequestParams(ThisUri.SERVER_YUMING + StudentUri.VIDEOPLAY);
        params.addHeader("cookie", sessionid);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.e("test", "老师拉取视频列表 ： " + result);
                if (result != null) {
                    Message msg = handler.obtainMessage();
                    msg.what = 1;
                    msg.obj = result;
                    handler.sendMessage(msg);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                swipeRefreshLayout.setRefreshing(true);
            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    String state = (String) msg.obj;
                    if (state != null && !state.equals("")) {
                        Gson gson = new Gson();
                        VideoTeacherEntity entity = null;
                        entity = gson.fromJson(state, VideoTeacherEntity.class);
                        list.addAll(entity.getList());
                        Toast.makeText(getApplication(), "获取老师视频成功！", Toast.LENGTH_SHORT).show();
                        swipeRefreshLayout.setRefreshing(false);
                        adapter.notifyDataSetChanged();
                    } else {
                        Toast.makeText(getApplication(), "获取老师视频失败，请重试！", Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        }
    };

    private void init() {
        sharedPreferences = VideoTeaching.this.getSharedPreferences("data_dyk", 0);
        lv_videoTeaching = (ListView) findViewById(R.id.lv_video_teacher);
        list = new ArrayList<>();
        adapter = new VideoTeachingAdapter(list, this);
        lv_videoTeaching.setAdapter(adapter);

        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.video_swiperefresh);
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light,
                android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
        swipeRefreshLayout.setOnRefreshListener(this);

        //设置自动刷新
        swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(true);
            }
        });
        loadData();


    }

    //返回按钮
    public void backs(View view) {
        finish();
    }

    @Override
    public void onRefresh() {
        list.clear();
        loadData();
    }
}


